<script setup>
  import {useRouter} from 'vue-router'
  import {useStore} from 'vuex'
  const props = defineProps(['menuData', 'index'])
  //创建router实例
  const router = useRouter()
  //创建store实例
  const store = useStore()
  //点击菜单
  const handleClick = (item,active) => {
    store.commit('addMenu',item.meta)
    store.commit('updateMenuActive',active)
    router.push(item.meta.path)
  }
</script>

<template>
  <template v-for="(item, index) in props.menuData">
    <el-menu-item @click="handleClick(item, `${props.index}-${item.meta.id}`)" :index="`${props.index}-${item.meta.id}`"
      v-if="!item.children || item.children.length === 0" :key="`${props.index}-${item.meta.id}`">
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <el-sub-menu :index="`${props.index}-${item.meta.id}`" v-else>
      <template #title>
        <el-icon>
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <tree-menu :index="`${props.index}-${item.meta.id}`" :menuData="item.children"></tree-menu>
    </el-sub-menu>
  </template>

</template>

<style scoped></style>
